package com.property.manage.service.fee;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guardchina.framework.mybatis.model.CustomPage;
import com.guardchina.framework.util.exception.BusinessException;
import com.guardchina.framework.util.utils.DateUtil;
import com.property.manage.dao.fee.CollectionMapper;
import com.property.manage.model.fee.Collection;
import com.property.manage.model.fee.CollectionVo;
import com.property.manage.model.fee.Property;
import com.property.manage.model.fee.PropertyVo;
import com.property.manage.model.rent.RentVo;
import com.property.manage.model.resource.BuildingVo;
import com.property.manage.service.rent.RentService;
import com.property.manage.service.resource.BuildingService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;

/**
 * 描述
 *
 * @author lwb
 * @since 2020/7/16
 */
@Component
public class CollectionService {
    @Value("${timed.tasks}")
    private Integer tasks;
    private CollectionMapper collectionMapper;
    @Autowired
    @Lazy
    private PropertyService propertyService;
    @Autowired
    @Lazy
    private BuildingService buildingService;
    @Autowired
    @Lazy
    private RentService rentService;

    public void saveOrUpdate(Integer type, Integer relationId, LocalDate nextPayDate) {
        Collection collection = getCollection(type, relationId);
        Integer village = getVillage(type, relationId);
        if (collection != null) {
            collection.setNextPayDate(nextPayDate);
            collection.setVillageId(village);
            collectionMapper.updateById(collection);
        } else {
            collection = new Collection();
            collection.setType(type);
            collection.setRelationId(relationId);
            collection.setNextPayDate(nextPayDate);
            collection.setVillageId(village);
            collectionMapper.insert(collection);
        }
    }

    public void delete(Integer type, Integer relationId) {
        UpdateWrapper<Collection> wrapper = new UpdateWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("relation_id", relationId);
        collectionMapper.delete(wrapper);
    }


    private Collection getCollection(Integer type, Integer relationId) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("relation_id", relationId);
        return collectionMapper.selectOne(wrapper);
    }


    @Autowired
    public void setCollectionMapper(CollectionMapper collectionMapper) {
        this.collectionMapper = collectionMapper;
    }


    public CustomPage<CollectionVo> getPage(Long pageNo, Long pageSize, String nextPayDate) {
        Page<CollectionVo> page = new Page<>(pageNo, pageSize);
        IPage<CollectionVo> result = collectionMapper.getPage(page, nextPayDate, DateUtil.getDateAfter(DateUtil.fromLocalDate(LocalDate.now()),tasks));
        List<CollectionVo> records = result.getRecords();
        if (!records.isEmpty()) {
            for (CollectionVo record : records) {
                Integer relationId = record.getRelationId();
                Integer type = record.getType();
                if (type == 1) {
                    //物业管理费
                    if (wuye(record, relationId)) continue;
                }
                if (type == 2 || type == 3) {
                    zujin(record, relationId, type);
                }
            }
        }

        return new CustomPage<>(result);
    }

    private Integer getVillage(Integer type, Integer relationId){
        if (type==1){
            Property property = propertyService.getById(relationId);
            if (property==null){
                throw new BusinessException("类型和管理id错误",String.format("类型[%s]和物业管理关联id[%S]",type,relationId));
            }
            BuildingVo building = buildingService.findBuilding(property.getBuildingId());
            if (building==null){
                throw new BusinessException("楼栋信息错误",String.format("楼栋id[%s]",property.getBuildingId()));

            }
            return building.getVillageId();
        }else {
            RentVo parking = rentService.findParking(relationId);
            if (parking==null){
                throw new BusinessException("类型和管理id错误",String.format("类型[%s]和租金管理关联id[%S]",type,relationId));
            }
            return parking.getVillageId();
        }
    }

    private void zujin(CollectionVo record, Integer relationId, Integer type) {
        RentVo parking = rentService.findParking(relationId);
        if (parking==null){
            return;
        }
        if (parking.getType() == 2) {
            record.setListOfCharges("房屋租金");
        }
        if (type == 3) {
            record.setListOfCharges("车位租金");
        }
        record.setVillageName(parking.getVillageName());
        record.setAddress(parking.getNumber());
        record.setName(parking.getName());
        record.setPhone(parking.getPhone());
        record.setMoney(parking.getMoney());
    }

    private boolean wuye(CollectionVo record, Integer relationId) {
        record.setListOfCharges("物业管理费");
        //调用物业管理接口查询付款方信息
        Property property = propertyService.getById(relationId);
        if (property == null) {
            return true;
        }
        BuildingVo building = buildingService.findBuilding(property.getBuildingId());
        if (building == null) {
            return true;
        }
        record.setMoney(property.getShouldPay());
        record.setPhone(property.getMobile());
        record.setName(property.getName());
        record.setAddress(buildingService.findAddress(property.getBuildingId()));
        record.setVillageName(building.getVillageName());
        return false;
    }


    public Integer getCount(Integer villageId){
        String date = DateUtil.fromLocalDate(LocalDate.now());
        return collectionMapper.getCount(DateUtil.getDateAfter(date,tasks),villageId);
    }
}
